using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace OpenComposite.EII.Designer.Workflow.Forms
{
	public partial class FormUIConfigMethod : Form
	{
		#region Initialization
		public FormUIConfigMethod(Repository.WorkflowMethod wfmethod)
		{
			if ( wfmethod == null ) throw new ArgumentNullException("wfmethod");
			InitializeComponent();

			_wfmethod = wfmethod;

			if ( _wfmethod != null ) {
				// display workflow method
				txtMethodName.Text = _wfmethod.Name;
				txtMethodInput.Text = _wfmethod.InputBusinessObjectName;
				txtMethodOutput.Text = _wfmethod.OutputBusinessObjectName;
			}

			displayMashupControlInfos();

			flowUIConfig.Size = groupUIConfig.ClientSize;
			tableControl.Width = flowUIConfig.ClientSize.Width - tableControl.Left * 2;
			groupColumns.Width = flowUIConfig.ClientSize.Width - groupColumns.Left * 2;
			groupDataMember.Width = flowUIConfig.ClientSize.Width - groupDataMember.Left * 2;
			groupDisplayValueMember.Width = flowUIConfig.ClientSize.Width - groupDisplayValueMember.Left * 2;
		}
		#endregion

		#region Public Members

		#region Properties
		#endregion

		#region Events
		#endregion

		#region Methods
		#endregion

		#endregion

		#region Private Members

		#region Variables
		private Repository.WorkflowMethod _wfmethod;
		#endregion

		#region Methods
		private void displayMashupControlInfos()
		{
			if ( _wfmethod.MashupControl == null ) {
				this.SuspendLayout();
				groupColumns.Visible = false;
				groupDataMember.Visible = false;
				groupDisplayValueMember.Visible = false;
				this.ResumeLayout(true);
				return;
			}

			txtControlName.Text = _wfmethod.MashupControl.Name;
			txtControlType.Text = _wfmethod.MashupControl.ControlType;

			this.SuspendLayout();
			groupColumns.Visible = false;
			groupDataMember.Visible = false;
			groupDisplayValueMember.Visible = false;
			switch ( _wfmethod.MashupControl.ControlType ) {
				case "System.Windows.Forms.TextBox":
				case "System.Windows.Forms.CheckBox":
				case "System.Windows.Forms.Label":
					groupDataMember.Visible = true;
					txtDataMember.Text = _wfmethod.MashupControl.DataMember;
					break;
				case "System.Windows.Forms.ComboBox":
				case "System.Windows.Forms.ListBox":
				case "System.Windows.Forms.CheckedListBox":
					groupDisplayValueMember.Visible = true;
					txtDisplayMember.Text = _wfmethod.MashupControl.DisplayMember;
					txtValueMember.Text = _wfmethod.MashupControl.ValueMember;
					break;
				case "System.Windows.Forms.DataGridView":
					groupColumns.Visible = true;
					txtColumns.Text = _wfmethod.MashupControl.GridColumns;
					break;
			}
			this.ResumeLayout(true);
		}
		#endregion

		#region Event Handlers
		private void tsbtnOK_Click(object sender, EventArgs e)
		{
			if ( txtControlName.Text != _wfmethod.MashupControl.Name ) {
				_wfmethod.Workflow.ExtendedProperties.MashupControls.Remove(_wfmethod.MashupControl.Name);
				_wfmethod.MashupControl.Name = txtControlName.Text;
				_wfmethod.Workflow.ExtendedProperties.MashupControls.Add(_wfmethod.MashupControl.Name, _wfmethod.MashupControl);
			}
			if ( groupDataMember.Visible ) {
				_wfmethod.MashupControl.DataMember = txtDataMember.Text;
			} else if ( groupDisplayValueMember.Visible ) {
				_wfmethod.MashupControl.DisplayMember = txtDisplayMember.Text;
				_wfmethod.MashupControl.ValueMember = txtValueMember.Text;
			} else if ( groupColumns.Visible ) {
				_wfmethod.MashupControl.GridColumns = txtColumns.Text;
			}
			this.DialogResult = DialogResult.OK;
			this.Close();
		}
		private void tsbtnCancel_Click(object sender, EventArgs e)
		{
			this.DialogResult = DialogResult.Cancel;
			this.Close();
		}
		#endregion

		private void btnSelectExistingControl_Click(object sender, EventArgs e)
		{
			FormSelectExistingMashupControl frm = new FormSelectExistingMashupControl(_wfmethod.Workflow, MashupUIConfigType.Method);
			if ( frm.ShowDialog() == DialogResult.OK ) {
				_wfmethod.MashupControl = frm.SelectedMashupControl;
				displayMashupControlInfos();
			}
		}

		private void btnCreateNewControl_Click(object sender, EventArgs e)
		{
			FormCreateMashupControl frm = new FormCreateMashupControl(_wfmethod.Workflow, MashupUIConfigType.Method);
			if ( frm.ShowDialog() == DialogResult.OK ) {
				_wfmethod.MashupControl = new OpenComposite.EII.Repository.Workflow.MashupControl();
				_wfmethod.MashupControl.Name = frm.ControlName;
				_wfmethod.MashupControl.ControlType = frm.ControlType.ToString();
				_wfmethod.Workflow.ExtendedProperties.MashupControls.Add(frm.ControlName, _wfmethod.MashupControl);
				displayMashupControlInfos();
			}
		}

		private void btnSelectDataMember_Click(object sender, EventArgs e)
		{

		}

		private void btnSelectDisplayMember_Click(object sender, EventArgs e)
		{

		}

		private void btnSelectValueMember_Click(object sender, EventArgs e)
		{

		}
		#endregion

	}
}